<%--
 *  File:    ComboCodeSegEdit.jsp
 *  @Author: Hilldao
 *  Date:    2002/1/8
 *
 *  Copyright (C) 2000 - 2001 Oval Technologies, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Oval
 *  Technologies, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Oval.
--%>

<%@ include file="/inc/SessionTimeout.inc"%>
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.log4j.Category" %>
<%@ page import="com.oval.util.OvalLabel" %>
<%@ page import="com.oval.util.OvalCustomColumn" %>
<%@ page import="com.oval.util.OvalCustomMessage" %>

<%@ page contentType = "text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/PageFlow.tld" prefix="PageFlow" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/oval-tag.tld" prefix="oval" %>
<%@ taglib uri="/WEB-INF/custom.tld" prefix="custom" %>

<%
  //init param
  Category cat = Category.getInstance("jsp.ComboCodeSegEdit");
  String strModuleName = "SPM";
  String privilegeCode = "Z0607,Z0614";
%>



<%
  HashMap userInfo = (HashMap)session.getAttribute("userinfo");
  String isFinish = (String)session.getAttribute("isfinishsession");

  if(isFinish==null)
  {
    isFinish="";
  }

  HashMap relationtoedit = new HashMap();
  boolean isnewEdit = false;
  if(request.getParameter("segmentid")!=null)
  {
     relationtoedit.put("segmentid",Integer.valueOf(request.getParameter("segmentid")));
     relationtoedit.put("languagecode",request.getParameter("languagecode"));
     isnewEdit = true;
  }
  if(session.getAttribute("relationsession")==null)
  {
     if(request.getParameter("fieldid")!=null)
       relationtoedit.put("fieldid",Integer.valueOf(request.getParameter("fieldid")));
     session.setAttribute("relationsession",relationtoedit);
  }

  HashMap ComboCodeSegSessionMap = (HashMap)session.getAttribute("relationsession");
%>
<% //--------------------------------------  include files list --%>
<%//@ include file="/inc/Security.inc" %>
<%@ include file="/inc/PageDefault.inc" %>

<html>
<head>
<title></title>
<PageFlow:CheckPageFlow pageName="combocodesegedit"/>
<%@ include file="/inc/InitEdit.inc" %>
<%
  HashMap ComboCodeSegMap = (HashMap)pageContext.getAttribute("combocodeseginfo");
  OvalCustomTable table = new OvalCustomTable(strLanguageCode);
  OvalCustomColumn column = new OvalCustomColumn(strLanguageCode);
  if(ComboCodeSegSessionMap!=null&&(!isnewEdit))
  {
    ComboCodeSegMap = ComboCodeSegSessionMap;
  }
  if(ComboCodeSegMap==null)
  {
    ComboCodeSegMap = new HashMap();
  }
  pageContext.setAttribute("combocodeseginfo",ComboCodeSegMap);


  Vector allsources = (Vector)pageContext.getAttribute("allvaluesources");
  if(allsources == null)
  {
    allsources = new Vector();
  }
  Vector alltables = (Vector)pageContext.getAttribute("alltables");
  if(alltables == null)
  {
    alltables = new Vector();
  }
  Vector allfields = (Vector)pageContext.getAttribute("allfields");
  if(allfields == null)
  {
    allfields = new Vector();
  }


  cat.debug("The ComboCodeSegMap is :" + ComboCodeSegMap);
  cat.debug("The allsources is :" + allsources);
  cat.debug("The alltables is :" + alltables);
  cat.debug("The allfields is :" + allfields);


  Integer sum = new Integer(0);
  if(pageContext.getAttribute("segwidthsum")!=null&&!isFinish.equals("Y"))
  {
    HashMap sumTemp = (HashMap)(((Vector)pageContext.getAttribute("segwidthsum")).get(0));
    if(sumTemp!=null)
	  {
	    sum = (Integer)sumTemp.get("sum");
	    if(sum==null)
	      sum = new Integer(0);
	  }
  }
  int maxs = 16 - sum.intValue();
 %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../css/<%= strLanguageCode %>/main.css" type="text/css">
<script language="JavaScript" src="../../js/FuncLib.js"></script>
<script>

<%if(isFinish.equals("Y") || maxs == 0){%>
   opener.closeChild();
<%}%>
function checkFormat(form)
{
  if(!isNumber(form.segmentno.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_INT", label.getLabelName("L_Sequence",false)) %>");
    return false;
  }
  if(!isNumber(form.segmentwidth.value))
  {
    alert("<%= message.getMessageDesc("VALUE_MUST_BE_INT", label.getLabelName("L_Length",false)) %>");
    return false;
  }
  return true;
}
function checkpass(form)
{
  if(form.valuesource.value == 1)
  {
		  if(form.tablename=="-1")
		  {
        alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_TableName",false)) %>");
		    return false;
		  }
		  if(form.fieldname=="-1")
		  {
        alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_Field",false)) %>");
		    return false;
		  }
	}
	else if(form.valuesource.value == 4)
	{
		  if(isEmpty(form.funcname.value))
		  {
        alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_FunctionName",false)) %>");
		    return false;
		  }
	}


	if(isEmpty(form.segmentno.value))
	{
        alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_Sequence",false)) %>");
		    return false;
	}
	if(isEmpty(form.segmentwidth.value))
	{
        alert("<%= message.getMessageDesc("VALUE_NOT_NULL", label.getLabelName("L_Length",false)) %>");
		    return false;
	}
  return true;
}

function change_table()
{
  document.combocodesegeditform.actionname.value = "reload";
	document.combocodesegeditform.target = "_self";
	document.combocodesegeditform.submit();
}
function change_valuesource()
{
  if(document.combocodesegeditform.valuesource.value == 1)
  {
    functionTr.style.display = "none";
    tableTr.style.display = "block";
    document.combocodesegeditform.tablename.disabled=false;
    document.combocodesegeditform.fieldname.disabled=false;
  }
  else if(document.combocodesegeditform.valuesource.value == 2 || document.combocodesegeditform.valuesource.value == 3)
  {
    functionTr.style.display = "none";
    tableTr.style.display = "none";
    document.combocodesegeditform.tablename.disabled=true;
    document.combocodesegeditform.fieldname.disabled=true;
  }
  else if(document.combocodesegeditform.valuesource.value == 4)
  {
    functionTr.style.display = "block";
    tableTr.style.display = "none";
    document.combocodesegeditform.tablename.disabled=true;
    document.combocodesegeditform.fieldname.disabled=true;
  }
}
function checksumlength(form)
{
   if(form.segwidthsum.value > 16 - form.segmentwidth.value)
   {
     alert("<%= message.getMessageDesc("VALUE_NOT_MORETHAN",label.getLabelName("L_Length",false),String.valueOf(maxs)) %>");
     return false;
   }
   return true;
}
function page_done()
{
  if(checkNull(combocodesegeditform))
    emptyToNull(combocodesegeditform);
  else
    return;

  if(!checkpass(document.combocodesegeditform))
    return;

  if(!checkFormat(document.combocodesegeditform))
    return;

  if(!checksumlength(document.combocodesegeditform))
    return;
  <%--[SCRIPT]  alert("fieldid->"+opener.document.combocodesegform.fieldid.value);  [SCRIPT]--%>
  if(document.combocodesegeditform.tablename.value==null)
    document.combocodesegeditform.tablename.disabled == true;
  if(document.combocodesegeditform.fieldname.value==null)
    document.combocodesegeditform.fieldname.disabled == true;

  document.combocodesegeditform.fieldid.value = opener.document.combocodesegform.fieldid.value;
  document.combocodesegeditform.isfinishsession.value = 'Y';
	document.combocodesegeditform.actionname.value = "done";
	document.combocodesegeditform.target = "_parent";
	document.combocodesegeditform.submit();
}
</script>
</head>
<body>


<table width="100%" cellspacing="0" border="0" cellpadding="0">
  <form name="combocodesegeditform" method="post" action="../../Dispatcher" target="_parent">
   <input type="hidden" name="pagename" value="combocodesegedit">
			   <input type="hidden" name="actionname">
				 <input type="hidden" name="datelastupdated" value="<%=JspHelper.filterNull( new java.sql.Date(new java.util.Date().getTime()) )%>">
         <input type="hidden" name="orgid" value="<%=JspHelper.filterNull( userInfo.get("orgid") )%>">
         <input type="hidden" name="languagecode" value="<%= strLanguageCode %>">
         <input type="hidden" name="segmentid" value="<%=JspHelper.filterNull( ComboCodeSegMap.get("segmentid") )%>">
         <input type="hidden" name="fieldid" value="<%=JspHelper.filterNull( ComboCodeSegMap.get("fieldid")!=null?ComboCodeSegMap.get("fieldid"):request.getParameter("fieldid"))%>">
         <input type="hidden" name="isfinishsession" >
         <input type="hidden" name="segwidthsum" value="<%=JspHelper.filterNull(sum)%>" >



  <tr>
    <td>
      <table width="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td colspan="2" class="summary_top"></td>
        </tr>
        <tr>
          <td colspan="2"><img src="../../images/<%= strLanguageCode %>/quote.gif" width="16" height="11">
            <%= label.getLabelName("L_NESegment") %><%= label.getLabelName("L_Colon") %></td>
        </tr>
      </table>
      <table width="100%" border="1" cellpadding="0" bordercolorlight="#999999" bordercolordark="#FFFFFF" cellspacing="0" align="center">
        <tr>
          <td class="need" width="80"><%= label.getLabelName("L_Sequence") %><%= label.getLabelName("L_Colon") %></td>
          <td>
            <input type="text" name="segmentno" value="<%=JspHelper.filterNull(ComboCodeSegMap.get("segmentno"))%>" size="20">

          </td>
          <td class="need"><%= label.getLabelName("L_Length") %><%= label.getLabelName("L_Colon") %></td>
          <td>
            <input type="text" name="segmentwidth" value="<%=JspHelper.filterNull(ComboCodeSegMap.get("segmentwidth"))%>" size="20">
          </td>
        </tr>

        <tr>
          <td class="td-text"><%= label.getLabelName("L_Description") %><%= label.getLabelName("L_Colon") %></td>
          <td>
            <input type="text" name="segmentdesc" value="<%=JspHelper.filterNull(ComboCodeSegMap.get("segmentdesc"))%>" size="40">
          </td>
          <td class="need"><%= label.getLabelName("L_ValueSource") %><%= label.getLabelName("L_Colon") %></td>
          <td>
           <%if(ComboCodeSegMap.size()==0){%>
             <oval:select optionsName="allvaluesources" optionValueKey="value" optionInnerTextKey="valuename" onchange="javascript:change_valuesource()" property="valuesource"/>
           <%}else{%>
             <oval:select optionsName="allvaluesources" optionValueKey="value" optionInnerTextKey="valuename" name="combocodeseginfo" onchange="javascript:change_valuesource()" property="valuesource"/>
           <%}%>
          </td>
        </tr>
        <tr id="tableTr">
          <td class="need"><%= label.getLabelName("L_TableName") %><%= label.getLabelName("L_Colon") %></td>
          <td>
         <%
	                 String strSelect = "";
	              %>
	              <select name="tablename" onchange="javascript:change_table()">
	              <option value="-1">-----------</option>
			          <logic:iterate id="atable" collection="<%= alltables %>">
			          <%
			                HashMap a_Table = (HashMap)pageContext.getAttribute("atable");
			                String tempName = (String)a_Table.get("tablename");
			                if(ComboCodeSegMap.get("tablename")!=null && ComboCodeSegMap.get("tablename").toString().trim().equalsIgnoreCase(tempName.trim()))
		                  {
		                    strSelect = "Selected";
		                  }
		                  else
		                  {
		                    strSelect = "";
		                  }
			              %>
	              <option value = "<%=JspHelper.filterNull( a_Table.get("tablename") )%>" <%=JspHelper.filterNull( strSelect )%>>
	                 <%=JspHelper.filterNull( table.getTable((String)a_Table.get("tablename")) )%>
	              </option>
	              </logic:iterate>
	              </select>
          </td>
          <%
	                 strSelect = "";
	        %>
          <td class="need"><%= label.getLabelName("L_Field") %><%= label.getLabelName("L_Colon") %></td>
          <td>
           <select name="fieldname">
	              <option value="-1">-----------</option>
			            <logic:iterate id="field" collection="<%= allfields %>">
			              <%
			                String tablename = (String)ComboCodeSegMap.get("tablename");
			                if(tablename==null)
			                {
			                  tablename = "";
			                }
			                HashMap a_field = (HashMap)pageContext.getAttribute("field");
			                if(ComboCodeSegMap.get("fieldname")!=null&&(((String)a_field.get("columnname")).trim()).equalsIgnoreCase(((ComboCodeSegMap.get("fieldname")).toString()).trim()))
		                  {
		                    strSelect = "Selected";
		                  }
			              %>
	              <option value = "<%=JspHelper.filterNull( a_field.get("columnname") )%>" <%=JspHelper.filterNull( strSelect )%> >
	                 <%=JspHelper.filterNull( column.getColumnDesc(tablename,(String)a_field.get("columnname")) )%>
	              </option>
	              </logic:iterate>
	         </select>
          </td>
        </tr>
        <tr id="functionTr">
          <td class="need"><%= label.getLabelName("L_FunctionName") %><%= label.getLabelName("L_Colon") %></td>
          <td colspan="3">
            <input type="text" name="funcname" size="20" value="<%=JspHelper.filterNull(ComboCodeSegMap.get("funcname"))%>">
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td class="summary_top"></td>
  </tr>
  <tr>
    <td class="td_center">
    <a onclick="page_done()"><custom:button type="1" label="L_Finish" imgsrc="finish.gif" border="0" style="width:60"/></a>
    </td>
  </tr>
  </form>
</table>
<script>
change_valuesource();
</script>
</body>
</html>


